const { imgUrl } = getApp().globalData;
import { mineApi } from "@/utils/request/api/mine";
import Toast from '@vant/weapp/toast/toast';
Page({
  data: {
    imgUrl: imgUrl + "mine/",
    monitor: {
      id: 0,
      company_name: "", //企业名称
      monitor_type: 1, //监控方式，1-全部，2-投标，3-中标
      msg_send_type: 1, //消息发送方式，1-平台消息，2-微信公众号，3-邮件
      monitor_start: "", //监控开始时间
      monitor_end: "", //监控结束时间
      note: "", //备注
    },
    zb: '1',
    tb: '1',
    pickerShow: false,
    date: "",
  },
  changebiao(event) {
    const type = event.currentTarget.dataset.type;
    let value = event.detail.value;
    if (type === "zb") {
      this.setData({
        zb: value,
      });
    } else {
      this.setData({
        tb: value,
      })
    }
  },
  onDisplay() {
    this.setData({ pickerShow: true });
  },
  onClose() {
    this.setData({ pickerShow: false });
  },
  formatDate(date) {
    date = new Date(date);
    let m = date.getMonth() + 1;
    let d = date.getDate();
    m = m < 10 ? "0" + m : m;
    d = d < 10 ? "0" + d : d;
    return `${date.getFullYear()}/${m}/${d}`;
  },
  change(event) {
    console.log('event === ', event)
    let obj = JSON.parse(JSON.stringify(this.data.monitor));
    obj.company_name = event.detail.value;
    this.setData({
      monitor: obj,
    });
  },
  onConfirm(event) {
    const [start, end] = event.detail;
    let obj = JSON.parse(JSON.stringify(this.data.monitor));
    obj.monitor_start = this.formatDate(start).replaceAll('/', '-');
    obj.monitor_end = this.formatDate(end).replaceAll('/', '-');

    this.setData({
      pickerShow: false,
      monitor: obj,
      date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
    });
  },
  save() {
    let obj = JSON.parse(JSON.stringify(this.data.monitor));
    if(obj.company_name === '') {
      wx.showToast({
        title: '请输入企业名称',
        icon: 'none'
      })
      return
    }
    if(this.data.date === '') {
      wx.showToast({
        title: '请选择时间',
        icon: 'none'
      })
      return
    }
    
    mineApi.setMonitor(obj).then(res => {
      console.log('res === ', res);
      wx.showToast({
        title: '保存成功',
        icon: 'none'
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1,
        })
      }, 1000)
    })
  },
  delMonitor() {
    mineApi.delMonitor({
      id: this.data.monitor.id
    }).then(res => {
      console.log('res === ', res);
      wx.showToast({
        title: '删除成功',
        icon: 'none'
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1,
        })
      }, 1000)
    })
  },
  onLoad(options) {
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('edit', data => {
      console.log('data === ', data)
      let date = data.data.monitor_start.replaceAll('-','/') + ' - ' + data.data.monitor_end.replaceAll('-','/');
      this.setData({
        monitor: data.data,
        type: 'edit',
        date
      })
    });
  },
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onShareAppMessage() {
    return {
      title: "",
    };
  },
});
